package com.zero.yygh.hosp.controller.api;

import com.zero.yygh.common.exception.CommonException;
import com.zero.yygh.common.helper.HttpRequestHelper;
import com.zero.yygh.common.result.Result;
import com.zero.yygh.common.result.ResultCodeEnum;
import com.zero.yygh.hosp.service.DepartmentService;
import com.zero.yygh.hosp.service.HospitalService;
import com.zero.yygh.hosp.service.HospitalSetService;
import com.zero.yygh.hosp.service.ScheduleService;
import com.zero.yygh.model.hosp.Department;
import com.zero.yygh.model.hosp.Hospital;
import com.zero.yygh.model.hosp.Schedule;
import com.zero.yygh.vo.hosp.DepartmentQueryVo;
import com.zero.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author zero
 * @desc 对外开发的接口全部写在这里
 * @create 2021/3/26
 */
@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

  @Resource
  private HospitalService hospitalService;
  @Resource
  private HospitalSetService hospitalSetService;
  @Resource
  private DepartmentService departmentService;
  @Resource
  private ScheduleService scheduleService;

  @ApiOperation(value = "上传医院")
  @PostMapping("saveHospital")
  public Result<String> saveHospital(HttpServletRequest request) {
    Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
    // 必须参数校验 略
    String hoscode = (String) paramMap.get("hoscode");
    if (StringUtils.isEmpty(hoscode)) {
      throw new CommonException(ResultCodeEnum.PARAM_ERROR);
    }
    // 传输过程中“+”转换为了“ ”，因此我们要转换回来
    String logoDataString = (String) paramMap.get("logoData");
    if (!StringUtils.isEmpty(logoDataString)) {
      String logoData = logoDataString.replaceAll(" ", "+");
      paramMap.put("logoData", logoData);
    }
    // 签名校验
    if (!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode))) {
      throw new CommonException(ResultCodeEnum.SIGN_ERROR);
    }

    hospitalService.save(paramMap);
    return Result.ok();
  }

  @ApiOperation(value = "获取医院信息")
  @PostMapping("hospital/show")
  public Result<Hospital> getHospital(HttpServletRequest request) {
    Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
    // 必须参数校验 略
    String hoscode = (String) paramMap.get("hoscode");
    if (StringUtils.isEmpty(hoscode)) {
      throw new CommonException(ResultCodeEnum.PARAM_ERROR);
    }
    // 签名校验
    if (!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode))) {
      throw new CommonException(ResultCodeEnum.SIGN_ERROR);
    }
    return Result.ok(hospitalService.getByHoscode((String) paramMap.get("hoscode")));
  }

  @ApiOperation(value = "上传科室")
  @PostMapping("saveDepartment")
  public Result<String> saveDepartment(HttpServletRequest request) {
    Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
    // 必须参数校验 略
    String hoscode = (String) paramMap.get("hoscode");
    if (StringUtils.isEmpty(hoscode)) {
      throw new CommonException(ResultCodeEnum.PARAM_ERROR);
    }
    // 签名校验
    if (!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode))) {
      throw new CommonException(ResultCodeEnum.SIGN_ERROR);
    }

    departmentService.save(paramMap);
    return Result.ok();
  }

  @ApiOperation(value = "获取分页列表")
  @PostMapping("department/list")
  public Result<Page<Department>> department(HttpServletRequest request) {
    Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
    // 必须参数校验 略
    String hoscode = (String) paramMap.get("hoscode");
    // 非必填
    String depcode = (String) paramMap.get("depcode");
    int page =
        StringUtils.isEmpty(paramMap.get("page"))
            ? 1
            : Integer.parseInt((String) paramMap.get("page"));
    int limit =
        StringUtils.isEmpty(paramMap.get("limit"))
            ? 10
            : Integer.parseInt((String) paramMap.get("limit"));

    if (StringUtils.isEmpty(hoscode)) {
      throw new CommonException(ResultCodeEnum.PARAM_ERROR);
    }
    // 签名校验
    if (!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode))) {
      throw new CommonException(ResultCodeEnum.SIGN_ERROR);
    }

    DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
    departmentQueryVo.setHoscode(hoscode);
    departmentQueryVo.setDepcode(depcode);
    Page<Department> pageModel = departmentService.selectPage(page, limit, departmentQueryVo);
    return Result.ok(pageModel);
  }

  @ApiOperation(value = "删除科室")
  @PostMapping("department/remove")
  public Result<String> removeDepartment(HttpServletRequest request) {
    Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
    // 必须参数校验 略
    String hoscode = (String) paramMap.get("hoscode");
    // 必填
    String depcode = (String) paramMap.get("depcode");
    if (StringUtils.isEmpty(hoscode)) {
      throw new CommonException(ResultCodeEnum.PARAM_ERROR);
    }
    // 签名校验
    if (!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode))) {
      throw new CommonException(ResultCodeEnum.SIGN_ERROR);
    }

    departmentService.remove(hoscode, depcode);
    return Result.ok();
  }

    @ApiOperation(value = "上传排班")
    @PostMapping("saveSchedule")
    public Result<String> saveSchedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
        if(StringUtils.isEmpty(hoscode)) {
            throw new CommonException(ResultCodeEnum.PARAM_ERROR);
        }
        //签名校验
        if(!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode))) {
            throw new CommonException(ResultCodeEnum.SIGN_ERROR);
        }
        scheduleService.save(paramMap);
        return Result.ok();
    }

    @ApiOperation(value = "获取排班分页列表")
    @PostMapping("schedule/list")
    public Result<Page<Schedule>> schedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
        //非必填
        String depcode = (String)paramMap.get("depcode");
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String)paramMap.get("limit"));

        if(StringUtils.isEmpty(hoscode)) {
            throw new CommonException(ResultCodeEnum.PARAM_ERROR);
        }
        //签名校验
        if(!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode))) {
            throw new CommonException(ResultCodeEnum.SIGN_ERROR);
        }

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        Page<Schedule> pageModel = scheduleService.selectPage(page , limit, scheduleQueryVo);
        return Result.ok(pageModel);
    }

    @ApiOperation(value = "删除科室")
    @PostMapping("schedule/remove")
    public Result<String> removeSchedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
        //必填
        String hosScheduleId = (String)paramMap.get("hosScheduleId");
        if(StringUtils.isEmpty(hoscode)) {
            throw new CommonException(ResultCodeEnum.PARAM_ERROR);
        }
        //签名校验
        if(!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode))) {
            throw new CommonException(ResultCodeEnum.SIGN_ERROR);
        }

        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }



}
